<?php
namespace Admin\Controller;
use Think\Controller;

/**
 * 处理问卷相关请求
 */
class TopicController extends BaseController
{
	public function index(){
	$data=M("topic")->select();
	$this->assign("topic",$data);
	$this->display();
	}
 /* 问卷列表 */
    public function questionnaireslist()
    {
    	
    	$data = M('Questionnaires')->where("iswp=0")->alias('q')->join('left join wpsk_company c on q.type=c.id')->field('q.nickname as nickname,q.ispub as ispub,q.id as id,c.name as type,q.name as name,q.description as description ,q.create_date as create_date,q.expire_date as expire_date,q.qrcode as qrcode')->select();
        $this->assign('questionnaires',$data);
		
      $this->display();
    }
	/**
	 * 增加问卷
	 */
	public function addquestionnaire(){
	if( IS_POST ){ //访问页面
      
            $questionnaires = M('Questionnaires');
         	
				$data ["nickname"] = $_SESSION["nickname"];
				$data ["ispub"] = I("post.ispub");
				$data ["name"] = I("post.name");
				$data ["iswp"] = 0;
				$data['nickname']=$_SESSION["nickname"];
				$data['openid']=111;
				$data ["description"] =I("post.description"); 
				$data ["create_date"] = date("Y-m-d",time()); 
				$data ["expire_date"] = I("post.expire_date");;
                $state = $questionnaires->add($data);

                if( $state===false ){
                    $this->error('问卷添加失败，错误信息：'.$questionnaires->getDbError());
                }else{
                   $this->success('问卷添加成功', U('topic/questionnaireslist'), 0);
                }
           
        }
        else{
        	
        	$this->assign("info",$data);
            $this->display();
        }
	}
	
 /* 问卷编辑 */
    public function editquestionnaire()
    {

        if( IS_GET ){ //访问页面
            $questionnaires = M('Questionnaires')->where("id='".I('get.id')."' and iswp=0")->field(true)->find();
          
        
            $this->assign($questionnaires);
            $this->display();
        }else{ //表单提交
        	   $questionnaires = M('Questionnaires');
        	$ordata=$questionnaires->where("id=".I("post.id"))->find();
        	
       	 if($ordata['openid']=="111"){
      		
      		$data['nickname']=$_SESSION["nickname"];
      		}
				$data ["name"] = I("post.name");
				$data ["ispub"] = I("post.ispub");
				$data ["description"] =I("post.description"); 
				$data ["create_date"] = date("Y-m-d",time()); 
				$data ["expire_date"] = I("post.expire_date");;
               
                $state = $questionnaires->where("id=".I("post.id"))->save($data);

                if( $state===false ){
                    $this->error('问卷更新失败，错误信息：'.$questionnaires->getDbError());
                }else{
                    $this->success('问卷编辑成功',  U('topic/questionnaireslist'), 0);
                }
          
        }
    }

    /* 问卷删除 */
    public function deletequestionnaire()
    {
        if( is_array(I('id')) ){ //批量删除
            $id = implode( ',', I('id', array(), 'intval') );
        }else{ //单个删除
            $id = I('id/d');
        }

        $questionnaires = D('Questionnaires');
        $state = $questionnaires->relation('Questions')->delete($id);

        if( $state===false ){
            $this->error('问卷删除失败，错误信息: '.$questionnaires->getDbError()); //删除问卷，并关联删除旗下问题
        }else{
            $this->redirect('/admin/topic/questionnaireslist');
        }
    }
	/**
	 * 评论
	 */
	public function comment(){
	$data=M("comment")->where("topic_id='".I('get.id')."'")->select();
	$this->assign("topic",$data);
	$this->display();
	}
	 /* 话题添加 */
    public function add()
    {
        if( IS_POST ){ //访问页面
         	$data ["pic"] = $this->upload ( $_FILES ['pic'] );
      		$data['type']=0;
      		$data['openid']=111;
      		$data['title']=$_POST['title'];
      		$data['addtime']=time();
      		$data['author']=$_SESSION["nickname"];
      		$data ["ispub"] = I("post.ispub");
      		$data['class']=$_POST['class'];
      		$data['headimg']=$_SESSION["user"]["admin_pic"];
      		if($data['type']==0){
      		$data['context']=$_POST['content'];
      		}
      		else {
      		$data['context']=$_POST['description'];
      		
      		$questionnaires = M('Questionnaires');
      			$qdata ["name"] =$data['title'];
      			$qdata ["iswp"] =0;
				$qdata ["description"] =$data['context']; 
				$qdata ["create_date"] = date("Y-m-d",time()); 
				$qdata ["ispub"] = I("post.ispub");
				$qdata ["expire_date"] = date("Y-m-d",time()+ (365 * 24 * 60 * 60));
				$qresult=$questionnaires->add($qdata);
			$data["questionnaires_id"]=$qresult;
      		}
      		$model = M('topic');
      		$state = $model->add($data);

                if( $state===false ){
                    $this->error('问卷添加失败，错误信息：'.$model->getDbError());
                }else{
                   $this->success('问卷添加成功', U('topic/index'), 0);
                }
           
        }
        else{
        	$dataclass=M("topicclass")->select();
        	$this->assign("class",$dataclass);
        
            $this->display();
        }
    }
    
     /* 话题编辑 */
    public function edit()
    {

        if( IS_GET ){ //访问页面
            $data = M('topic')->find( I('get.id') );
           $dataclass=M("topicclass")->select();
        	$this->assign("class",$dataclass);
        	$this->assign("info",$data);
            
            $this->display();
        }else{ //表单提交
        	$model = M('topic');
        	$ordata=$model->where("id=".I("post.id"))->find();
        	
        	if ($_FILES ['pic'] ['name'] != null)
         		$data ["pic"] = $this->upload ( $_FILES ['pic'] );
        	
      		$data['title']=$_POST['title'];
      		$data ["ispub"] = I("post.ispub");
      		$data['class']=$_POST['class'];
      		$data['addtime']=time();
      		if($ordata['openid']=="111"){
      		$data['headimg']=$_SESSION["user"]["admin_pic"];
      		$data['author']=$_SESSION["nickname"];
      		}
      		$data['type']=0;
      		$data['context']=$_POST['content'];
      	

                $state = $model->where("id=".I("post.id"))->save($data);

                if( $state===false ){
                    $this->error('问卷更新失败，错误信息：'.$model->getDbError());
                }else{
                    $this->success('问卷编辑成功', '/admin/topic/index', 0);
                }
          
        }
    }
    
  /**
     * 话题类型
     */
    public function topicclass(){
    		
		$data = M ( "topicclass" )->select ();
		
		
		$this->assign ( "class", $data );
		
		
		$this->display ();
    
    }
    
 /**
     * 增加话题类型
     */
    public function addclass(){
    		
      if( IS_GET ){ //访问页面
            $this->display();
        }else{ //表单提交
            $model = M('topicclass');
         	$data["name"]=$_POST["name"];
         
         	$data ["src"] = $this->upload ( $_FILES ['pic'] );
			
           
            $state = $model->add($data);

                if( $state===false ){
                    $this->error('分类添加失败，错误信息：'.$model->getDbError());
                }else{
                	
                	
                    $this->success('分类添加成功', U('Topic/topicclass'), 0);
                }
           
        }
    
    }
    
/**
     * 增加话题类型
     */
    public function editclass(){
    		
      if( IS_GET ){ //访问页面
      		$id=I("get.id");
      		$data = M ( "topicclass" )->find ($id);
      		$this->assign("info",$data);
            $this->display();
        }else{ //表单提交
            $model = M('topicclass');
            $data['id']=$_POST["id"];
         	$data["name"]=$_POST["name"];
         	if ($_FILES ['pic'] ['name'] != null)
         	$data ["src"] = $this->upload ( $_FILES ['pic'] );
			
           
            $state = $model->save($data);

                if( $state===false ){
                    $this->error('分类添加失败，错误信息：'.$model->getDbError());
                }else{
                	
                	
                    $this->success('分类修改成功', U('Topic/topicclass'), 0);
                }
           
        }
    
    }
    
	/**
     * 增加话题类型
     */
    public function addhot(){
    		
      if( IS_GET ){ //访问页面
      	$data=M("topic")->select();
      	$this->assign("topic",$data);
         $this->display();
        }else{ //表单提交
            $model = M('hottopic');
         	$data["topic_id"]=$_POST["id"];
            $state = $model->add($data);

                if( $state===false ){
                    $this->error('分类添加失败，错误信息：'.$model->getDbError());
                }else{
                    $this->success('分类添加成功', U('Topic/hottopic'), 0);
                }
        }
    
    }
    
/**
     * 增加话题类型
     */
    public function hottopic(){
    		
    $model=new \Think\Model;

        $data = $model->table("wpsk_hottopic h,wpsk_topic t")->where("h.topic_id=t.id")
        				->field('t.*,h.id as hot_id')->select();
        
      	$this->assign("info",$data);
         $this->display();
        
    
    }
/**
     * 增加话题类型
     */
    public function addhotquestionnaire(){
    		
      if( IS_GET ){ //访问页面
      	$data=M("questionnaires")->where("iswp=0")->select();
      	$this->assign("info",$data);
         $this->display();
        }else{ //表单提交
            $model = M('hotquestionnaire');
         	$data["questionnaire_id"]=$_POST["id"];
            $state = $model->add($data);

                if( $state===false ){
                    $this->error('分类添加失败，错误信息：'.$model->getDbError());
                }else{
                    $this->success('分类添加成功', U('Topic/hotquestionnaire'), 0);
                }
        }
    
    }
    
/**
     * 增加话题类型
     */
    public function hotquestionnaire(){
    		
    $model=new \Think\Model;

        $data = $model->table("wpsk_hotquestionnaire h,wpsk_questionnaires q")->where("h.questionnaire_id=q.id")
        				->field('q.*,h.id as hot_id')->select();
        
      	$this->assign("info",$data);
         $this->display();
        
    
    }
 /* 公司删除 */
    public function classdelete()
    {
        if( is_array(I('id')) ){ //批量删除
            $id = implode( ',', I('id', array(), 'intval') );
        }else{ //单个删除
            $id = I('id/d');
        }

        $company = D('topicclass');
	$state = $company->relation(true)->delete($id);
        if( $state===false ){
            $this->error('公司删除失败，错误信息: '.$company->getDbError()); //删除问卷，并关联删除旗下问题
        }else{
            redirect( U('topic/topicclass'));
        }
    }
	/* 话题删除 */
	public function delete()
	{
        if( is_array(I('id')) ){ //批量删除
            $id = implode(',', I('id', array(), 'intval'));
        }else{ //单个删除
            $id = I('id/d');
        }

        $questions = M('topic');
        $data_question=$questions->find($id);
        $questionnaires = D('Questionnaires');
        $state = $questionnaires->relation('Questions')->delete($data_question['questionnaires_id']);
        $state = $questions->delete($id);

        if( $state===false ){
	        $this->error('问卷删除失败，错误信息: '.$questions->getDbError());
        }else{
	        redirect(U('topic/index'));
        }
	}
 /* 公司删除 */
    public function hotdelete()
    {
        if( is_array(I('id')) ){ //批量删除
            $id = implode( ',', I('id', array(), 'intval') );
        }else{ //单个删除
            $id = I('id');
        }

        $company = M('hottopic');
		$state = $company->delete($id);
        if( $state===false ){
            $this->error('公司删除失败，错误信息: '.$company->getDbError()); //删除问卷，并关联删除旗下问题
        }else{
            redirect( U('topic/hottopic'));
        }
    }
 /* 公司删除 */
    public function hotquestionnairedelete()
    {
        if( is_array(I('id')) ){ //批量删除
            $id = implode( ',', I('id', array(), 'intval') );
        }else{ //单个删除
            $id = I('id');
        }

        $company = M('hotquestionnaire');
		$state = $company->delete($id);
        if( $state===false ){
            $this->error('公司删除失败，错误信息: '.$company->getDbError()); //删除问卷，并关联删除旗下问题
        }else{
            redirect( U('topic/hotquestionnaire'));
        }
    }
    
    /**
	 * 上传
	 * @param unknown_type $filename
	 * @return string
	 */
	public function upload($filename) {
		$upload = new \Think\Upload (); // 实例化上传类
		$upload->maxSize = 3145728; // 设置附件上传大小
		$upload->exts = array ('jpg', 'gif', 'png', 'jpeg' ); // 设置附件上传类型
		$upload->rootPath = './Public/Upload/'; // 设置附件上传根目录
		$upload->savePath = '';
		// 上传单个文件 
		$info = $upload->uploadOne ( $filename );
		if (! $info) { // 上传错误提示错误信息
			$this->error ( $upload->getError () );
			exit ();
		} else { // 上传成功 获取上传文件信息
			return $info ['savepath'] . $info ['savename'];
		}
	}
    
    
    
}